package com.chzc.fm.duotin.ui.channel;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.RadioLinearLayoutAdapter;
import com.chzc.fm.duotin.bean.channel.Rank;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.chzc.fm.duotin.widget.SpacesItemDecoration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/27.
 */
public class RadioActivity extends AppCompatActivity implements View.OnClickListener{

    private RecyclerView recyclerView;

    private ImageView ivback;

    private RadioLinearLayoutAdapter adapter;

    private List<Rank> radioList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);

        initViews();

        initDatas();

    }

    public void initViews()
    {
        recyclerView = (RecyclerView) findViewById(R.id.radio_conten_rv);
        ivback = (ImageView) findViewById(R.id.radio_back_iv);

        LinearLayoutManager manager = new LinearLayoutManager(RadioActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        recyclerView.addItemDecoration(new SpacesItemDecoration(30));


        ivback.setOnClickListener(this);


    }

    public void initDatas()
    {
        adapter = new RadioLinearLayoutAdapter(getApplicationContext(),radioList);
        recyclerView.setAdapter(adapter);

        getRadio(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {

                    LogUtil.w("开始解析数据");

                    JSONObject root = new JSONObject(object.toString());
                    String error_msg = root.getString("error_msg");

                    if ("SUCCESS".equals(error_msg))
                    {
                        JSONArray datalist = root.getJSONArray("data");

                        final List<Rank> radioList2 = Rank.arrayRankFromData(datalist.toString());

                        radioList.addAll(radioList2);

                        LogUtil.w("radiolist.size() = " + radioList.size());

                        adapter.notifyDataSetChanged();

                        adapter.setOnItemClickListener(new RadioLinearLayoutAdapter.mItemClickListener() {
                            @Override
                            public void onItemClick(int position) {

                                switch (position)
                                {
                                    default:
                                        Intent intent = new Intent(RadioActivity.this,LivePlayActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable("rankitem",radioList.get(position));
                                        intent.putExtras(bundle);
                                        startActivity(intent);
                                        break;
                                }
                            }
                        });
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }


    /**
     * 请求频道模块中电台页面数据
     * @param callback
     */
    public void getRadio(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request  =  new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                return HttpUtil.doGet(GlobalParam.URL_RADIO);
            }
        };
        new DuotinTask(request,callback).execute();
    }


    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.radio_back_iv:
                finish();
                break;
        }
    }
}
